package com.mangocheese.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.mangocheese.subject.application.convert.SubjectAnswerDTOConverter;
import com.mangocheese.subject.application.convert.SubjectInfoDTOConverter;
import com.mangocheese.subject.application.dto.SubjectInfoDTO;
import com.mangocheese.subject.common.entity.CommonResult;
import com.mangocheese.subject.domain.entity.SubjectAnswerBO;
import com.mangocheese.subject.domain.entity.SubjectInfoBO;
import com.mangocheese.subject.domain.service.SubjectInfoDomainService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 题目信息controller
 */
@RestController
@Slf4j
@RequestMapping("/subject/info")
public class SubjectInfoController {
    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 新增题目
     */
    @PostMapping("/add")
    public CommonResult<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.add.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDTO.getSubjectName()),
                    "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds())
                    , "分类id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds())
                    , "标签id不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convert(subjectInfoDTO);
            List<SubjectAnswerBO> subjectAnswerBOS =
                    SubjectAnswerDTOConverter.INSTANCE.convert(subjectInfoDTO.getOptionList());
            subjectInfoBO.setOptionList(subjectAnswerBOS);
            subjectInfoDomainService.add(subjectInfoBO);
            return CommonResult.ok(true);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return CommonResult.fail(false, "新增题目失败");
        }
    }

//    /**
//     * 查询题目列表
//     */
//    @PostMapping("/page")
//    public CommonResult<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
//        try {
//            if (log.isInfoEnabled()) {
//                log.info("SubjectController.getSubjectPage.dto:{}", JSON.toJSONString(subjectInfoDTO));
//            }
//            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类id不能为空");
//            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签id不能为空");
//            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convert(subjectInfoDTO);
//            subjectInfoBO.setPageNo(subjectInfoDTO.getPageNo());
//            subjectInfoBO.setPageSize(subjectInfoDTO.getPageSize());
//            PageResult<SubjectInfoBO> boPageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBO);
//            return CommonResult.ok(boPageResult);
//        } catch (Exception e) {
//            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
//            return CommonResult.fail(null,"分页查询题目失败");
//        }
//    }
//
//    /**
//     * 查询题目信息
//     */
//    @GetMapping("query")
//    public CommonResult<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
//        try {
//            if (log.isInfoEnabled()) {
//                log.info("SubjectController.querySubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDTO));
//            }
//            Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目id不能为空");
//            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convert(subjectInfoDTO);
//            SubjectInfoBO boResult = subjectInfoDomainService.queryInfo(subjectInfoBO);
//            SubjectInfoDTO dto = SubjectInfoDTOConverter.INSTANCE.convert(boResult);
//            return CommonResult.ok(dto);
//        } catch (Exception e) {
//            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
//            return CommonResult.fail(null,"查询题目详情失败");
//        }
//    }
}
